package com.sfd.test.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sfd.core.entity.Order;
import com.sfd.core.module.Result;
import com.sfd.core.service.UserService;
import com.sfd.test.feign.Test1FeignClient;
import com.sfd.test.feign.Test2FeignClient;
import com.sfd.test.mapper.OrderMapper;
import com.sfd.test.service.OrderService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;

/**
 * @author haden
 * @date 2022/8/8 18:18
 **/
@Service
@Slf4j
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    @Resource
    private Test1FeignClient test1FeignClient;
    @Resource
    private Test2FeignClient test2FeignClient;
    @Resource
    private UserService userService;

    @Override
    @GlobalTransactional(name = "order-create", rollbackFor = Exception.class)
    public void create() {
        Order order = new Order();
        order.setOrderNo(System.currentTimeMillis() + "");
        int num = 10;
        Long stockId = 1L;
        order.setStockId(stockId);
        order.setNum(num);
        order.setTime(new Date());
        save(order);
        Result<String> result = test1FeignClient.subStock(stockId, num);
        log.error("库存修改:" + JSONObject.toJSONString(result));
        Result<String> result1 = test2FeignClient.create(order.getId(), new BigDecimal(num));
        log.error("订单金额记录:" + JSONObject.toJSONString(result1));
//        HashMap map = new HashMap();
        if (!result.isSuccess() || !result1.isSuccess()) {
            int i = 1 / 0;
        }
    }
}
